<?php
include('db_and_modx_init.php');
include('core.inc.php');

$action = $_POST['action'];

$table_info = unserialize(stripslashes(urldecode($_POST['table_info'])));
$id = mysql_real_escape_string($_POST['id']);
$current_doc_id = mysql_real_escape_string($_POST['current_doc_id']);

switch ($action) {
	case 'remove':
	//todo: make this support different primary key column names
		$id = mysql_real_escape_string($_POST['id']);
		$modx->db->delete($table_info->getTableName(), $table_info->getPrimaryKey().'='.$id);
	break;
	
	case 'add':
$fields = Array(
		$table_info->getResouceIdColName() => $current_doc_id,
		$table_info->getPrimaryJoinedColName() => $id
	);
	
	foreach($table_info->getForeignKeys() as $column => $table){
		if (count_col($column) ){
			if (isset($_POST['tv'.$column])){ //hax for tmpvar.inc.php
				$fields = array_merge(array($column => mysql_real_escape_string($_POST['tv'.$column]) ), $fields);
			}else{
				echo "row move error: data for $column not provided!";
			}
		}
	}
	
		$modx->db->insert($fields, $table_info->getTableName());
	break;
}

function count_col($colName){
	global $table_info;
	return ($colName != $table_info->getResouceIdColName() && $colName != $table_info->getPrimaryJoinedColName() && $colName != $table_info->getPrimaryKey());
}

echo "200";

?>